class GoalCheckpoint < ActiveRecord::Base
  belongs_to :goal
  
  STATE_PENDING = 0
  STATE_SUCCESS = 1
  STATE_FAILURE = 2
  
  STATES = [STATE_PENDING, STATE_SUCCESS, STATE_FAILURE]
  
  validates_inclusion_of :state, 
    :in => [STATE_PENDING, STATE_SUCCESS, STATE_FAILURE]

  def change_state status
    self.state = status
    save
    
    if goal.completed != Goal::COMPLETE_POSITIVE && goal.progress.pending == 0
      goal.completed = Goal::COMPLETE_POSITIVE
      goal.save
    end
    
    true
  end

end
